# -*- coding: utf-8 -*-
from django.http import HttpResponse
import responseCode
try:
    import json     # python >= 2.6
except ImportError:
    import simplejson as json   # python <= 2.5


RES_CODE = responseCode.ResponseCode()


#   接口返回json
def response_json(data):
    return HttpResponse(json.dumps(data, ensure_ascii=False), content_type="application/json,charset=utf-8")


#   接口请求成功返回
def success(data=''):
    res = {
        "message": RES_CODE.SUCCESS["message"],
        "code": RES_CODE.SUCCESS["code"],
        "data": data
    }

    return response_json(res)


#   接口请求失败返回
def fail(type='', message=''):
    code_obj = getattr(RES_CODE, type, getattr(RES_CODE, 'FAILED'))

    res = {
        "message": code_obj["message"],
        "code": code_obj["code"],
        "data": message or code_obj["message"],
    }
    print res
    return response_json(res)


def request_is_empty(data):
    if (data is None) or (data.strip() == ''):
        return True
    else:
        return False